package com.leyou.item.controller;


import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import com.leyou.pojo.PageResult;
import com.leyou.utils.ResponseEntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 商品列表分页查询
     *
     * @param key      条件查询
     * @param saleable 是否上架
     * @param page     当前页码
     * @param rows     页面容量
     * @return SpuBo
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows) {
        PageResult<SpuBo> spuPageResult = this.goodsService.querySpuByPage(key, saleable, page, rows);
        return ResponseEntityUtils.selectResultStatus(spuPageResult);
    }


    /**
     * 商品列表 新增商品
     *
     * @param spuBo 商品信息
     * @return 无  只返回状态码
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        try {
            this.goodsService.saveGoods(spuBo);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            System.out.println("saveGoods运行异常");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


    /**
     * 商品列表修改商品 根据spuId查询对应的spuDetail 回显数据到前台页面
     *
     * @param spuId spu的id
     * @return
     */
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("spuId") Long spuId) {
        SpuDetail spuDetail = this.goodsService.querySpuDetailById(spuId);
        return ResponseEntityUtils.selectResultStatus(spuDetail);
    }


    /**
     * 商品列表页 修改商品 根据spuId查询对应的skus 回显数据到前台页面
     *
     * @param spuId
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId) {
        List<Sku> skus = this.goodsService.querySkuBySpuId(spuId);
        return ResponseEntityUtils.selectResultStatus(skus);
    }


    /**
     * 商品列表页 修改商品
     *
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo) {
        try {
            this.goodsService.updateGoods(spuBo);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


    /**
     * 商品列表上下架按钮点击事件
     *
     * @param spuId 改变上下架状态的spu id
     * @return 状态码
     */
    @GetMapping("spu/isSaleable")
    public ResponseEntity<Void> isSaleableBySpuId(@RequestParam("spuId") Long spuId) {
        try {
            this.goodsService.isSaleableBySpuId(spuId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.ACCEPTED).build();
        }
    }


    /**
     * 商品列表删除按钮点击事件
     *
     * @param spuId
     * @return
     */
    @DeleteMapping("spu/isDelete")
    public ResponseEntity<Void> isDeleteBySpuId(@RequestParam("spuId") Long spuId) {
        Boolean flag = this.goodsService.isDeleteBySpuId(spuId);
        if (flag) return ResponseEntity.ok(null);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 根据spu的id查询spu
     *
     * @param spuId
     * @return
     */
    @GetMapping("spu/{spuId}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("spuId") Long spuId) {
        Spu spu = this.goodsService.querySpuById(spuId);
        return ResponseEntityUtils.selectResultStatus(spu);
    }
}
